//
//  DLBaseVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/22.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLBaseVC.h"
#import "DLBaseTableView.h"
#import "DLNetWorkErrorView.h"
#import "UIImage+Utilities.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import "DLCustomTitleView.h"

@interface DLBaseVC ()

@property (nonatomic, strong) UIButton *backPreviousPageButton;
@property(nonatomic,strong) UIImageView *imageViewss;

@end

@implementation DLBaseVC

#pragma mark - LifeCycle
- (void)dealloc {
    NSLog(@"dealloc ======> %@", NSStringFromClass([self class]));
}

- (void)viewWillAppear:(BOOL)animated {
    kuserManager.lastVc = self;
    [super viewWillAppear:animated];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.hiddenNavigationBackground = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutSubcontrols];
    // Do any additional setup after loading the view.
}
- (void) layoutSubcontrols {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    if (self.navigationController.childViewControllers.count > 1) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backPreviousPageButton];
    }
    self.hiddenNavigationBackground = NO;
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[UITableViewCell alloc] init];
}

#pragma mark - PublicMethods

- (void)backPreviousPage {
    if (self.isPresent) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        if (self.navigationController.childViewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}


- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (UIImage *)navigationBackgroundImage {
//    NSString *imageName = @"normal_navbg";
//    if (iPhoneX) {
//        imageName = @"normal_navbg";
//    }
    NSString *imageName = @"normal_navbg";
    if (iPhoneX) {
        imageName = @"normal_navbg";
    }
    
    
    
    UIImage *image = [UIImage imageNamed:imageName];
    return image;
}

- (void)networkRetryyyy {
    self.showNetworkErrorView = NO;
}

#pragma mark - SetterGetter

- (void)setHiddenNavigationBottomLine:(BOOL)hiddenNavigationBottomLine {
    _hiddenNavigationBottomLine = hiddenNavigationBottomLine;
    
    if (_hiddenNavigationBottomLine) {
        [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
    }
    
//    UIImageView *imageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
//    imageView.hidden = hiddenNavigationBottomLine;
    
    
//    UIView *backgroundView = [self.navigationController.navigationBar subviews].firstObject;
//    for (UIView *view in backgroundView.subviews) {
//        if (CGRectGetHeight([view frame]) <= 2) {
//
//            view.hidden = YES;
//        }
//    }
    
    
}

- (void)setHiddenNavigationBackground:(BOOL)hiddenNavigationBackground {
    _hiddenNavigationBackground = hiddenNavigationBackground;
    if (hiddenNavigationBackground) {
        self.navigationController.navigationBar.translucent = YES;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        self.hiddenNavigationBottomLine = YES;
    } else {
        self.navigationController.navigationBar.translucent = NO;
        [self.navigationController.navigationBar setBackgroundImage:[self navigationBackgroundImage] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        self.hiddenNavigationBottomLine = NO;
        
        if (self.imageViewss) {
            [self.imageViewss removeFromSuperview];
            self.imageViewss = nil;
        }
        self.imageViewss = [[UIImageView alloc]initWithFrame:CGRectMake(0, -kHeightNavigation, kScreenWidth, kHeightNavigation)];
        [self.view addSubview:self.imageViewss];
        self.imageViewss.image = self.navigationBackgroundImage;
    }
}

- (void)setShowNetworkErrorView:(BOOL)showNetworkErrorView {
    _showNetworkErrorView = showNetworkErrorView;
    
    if (!showNetworkErrorView) {
        _networkErrorView.hidden = YES;
        return;
    }
    if (!_networkErrorView) {
        [self.view addSubview:self.networkErrorView];
    }
    
    [self.view bringSubviewToFront:self.networkErrorView];
    self.networkErrorView.hidden = NO;
}

- (DLBaseTableView *) plainTableView {
    
    if (!_plainTableView) {
        _plainTableView = [[DLBaseTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenSafeAreaHeight)];
        _plainTableView.delegate = self;
        _plainTableView.dataSource = self;
        _plainTableView.showsVerticalScrollIndicator = NO;
        _plainTableView.showsHorizontalScrollIndicator = NO;
        _plainTableView.estimatedRowHeight = 0;
        _plainTableView.tableFooterView = [UIView new];
        _plainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _plainTableView;
}

- (DLBaseTableView *) groupTableView {
    
    if (!_groupTableView) {
        _groupTableView = [[DLBaseTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenSafeAreaHeight) style:UITableViewStyleGrouped];
        _groupTableView.delegate = self;
        _groupTableView.dataSource = self;
        _groupTableView.showsVerticalScrollIndicator = NO;
        _groupTableView.showsHorizontalScrollIndicator = NO;
        _groupTableView.estimatedRowHeight = 0;
        _groupTableView.tableFooterView = [UIView new];
        _groupTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _groupTableView;
}

- (DLCustomTitleView *) titleView {
    if (!_titleView) {
        _titleView = [[DLCustomTitleView alloc] init];
        self.navigationItem.titleView = _titleView;
    }
    return _titleView;
}


- (UIButton *)backPreviousPageButton {
    if (!_backPreviousPageButton) {
        _backPreviousPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backPreviousPageButton.size = CGSizeMake(30, 30);
        [_backPreviousPageButton addTarget:self action:@selector(backPreviousPage) forControlEvents:UIControlEventTouchUpInside];
        [_backPreviousPageButton setImage:[UIImage imageNamed:@"nav_back_white"] forState:UIControlStateNormal];
//        [_backPreviousPageButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
    }
    return _backPreviousPageButton;
}

- (DLNetWorkErrorView *) networkErrorView {
    
    if (!_networkErrorView) {
        kWeakSelf;
        _networkErrorView = [[DLNetWorkErrorView alloc] initWithNetworkRetry:^{
            [weakSelf networkRetryyyy];
        }];
        _networkErrorView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }
    return _networkErrorView;
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleLightContent;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
